<%@ Import Namespace="Passageways" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Passageways.Logs" %>
<%@ Import Namespace="Passageways.Settings" %>
<%@ Import Namespace="Passageways.DirectoryServices.UserDirectory" %>
<%@ Import Namespace="Passageways.Modules.GroupCollaboration" %>
<%@ Register TagPrefix="pw" Namespace="Passageways.WebControls" Assembly="Passageways.WebControls"%>

<pw:Theme runat="server"/>

<SCRIPT Language="c#" runat="server">

void Page_Load(Object Src, EventArgs E)
{
	if(!Page.IsPostBack)
	{
		ReturnLink.Text = "<A class=portallink HREF='" + Request.Params["ReturnUrl"] + "'><< return to page</A>";
		
		//Populate TextBox with the current Url from the island's preference string
		url.Text = PWIslandInstance.GetPrefs(Request.Params["IslandInstanceID"]);
	}
}

void SaveSettings(Object Src, EventArgs E)
{
	//Save the url the user has specified in the island's preference string stored in the portal
	PWIslandInstance.StorePrefs(url.Text,Request.Params["IslandInstanceID"].ToString());
	
	//Display confirmation message
	CreateResult.Text = "The news feed URL has been saved.";
}

void TestUrl(Object Src, EventArgs E)
{
	GetRSSFeed(url.Text);
}

void GetRSSFeed(string site)
{
	try
	{
		bool versionFound = false;
		TitleImage.Text = "";
		Title.Text = "";
		Description.Text = "";


        WebRequest request = WebRequest.Create(site);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        
        Stream responseStream = response.GetResponseStream();

        XmlTextReader reader = new XmlTextReader(responseStream);
        XmlDocument doc = new XmlDocument();
        
        doc.Load(reader);

        //fetchXML.Document = doc;
        XmlNodeList xNodes = doc.ChildNodes;

        string mainTitle = "";
        string mainLink = "";
        string mainDesciption = "";

        DataTable dt = new DataTable();
        dt.Columns.Add("link");
        dt.Columns.Add("title");
        dt.Columns.Add("description");        
        
        foreach (XmlNode xNode in xNodes)
        {
            if (xNode.Name.ToLower().Equals("rss"))
            {
                if (xNode.Attributes["version"].Value == "2.0")
                    versionFound = true;
                
                foreach (XmlNode rss in xNode.ChildNodes[0].ChildNodes)
                {
                    if (rss.Name.ToLower() == "title")
                    {
                        mainTitle = rss.InnerText;
                    }

                    if (rss.Name.ToLower() == "link")
                    {
                        mainLink = rss.InnerText;
                    }

                    if (rss.Name.ToLower() == "description")
                    {
                        mainDesciption = rss.InnerText;
                    }

                    if (rss.Name.ToLower() == "item")
                    {
                        DataRow row = dt.NewRow();
                        
                        foreach (XmlNode item in rss.ChildNodes)
                        {
                            
                            if (item.Name.ToLower() == "title")
                            {
                                row["title"] = item.InnerText;
                                //Response.Write(item.InnerText + "<BR>");
                            }

                            if (item.Name.ToLower() == "link")
                            {
                                row["link"] = item.InnerText;
                            }

                            if (item.Name.ToLower() == "description")
                            {
                                row["description"] = item.InnerText;
                            }
                        }

                        dt.Rows.Add(row);   
                    }                    
                    
                }
            }
        }

        Title.Text = "<A class=portallink HREF='" + mainLink + "'>" + mainTitle + "</A>";
        Description.Text = mainDesciption;

        dg.DataSource = dt.DefaultView;

        if (versionFound)
        {
            dg.DataBind();
            r.Text = "<TABLE style='border:1px solid black;' width='100%' cellpadding=0 class=portalbodytext><TD><TD bgcolor=Beige><B>The RSS Url is valid if you can see the desired items below.</B></TD></TR></TABLE>";
        }
        else
        {
            r.Text = "<TABLE style='border:1px solid black;' width='100%' cellpadding=0 class=portalbodytext><TD><TD bgcolor=Beige><B>The RSS Url is not valid.  Only RSS version 2.0 is supported, make sure the url you typed is correct.</B></TD></TR></TABLE>";
        }        
        
	}
	catch(Exception exc)
	{
		r.Text = "<TABLE style='border:1px solid black;' width='100%' cellpadding=0 class=portalbodytext><TD><TD bgcolor=Beige><B>Error displaying the url you typed in:</B>:<BR>Feed URL: " + site + "<BR>" + exc.ToString() + "</TD></TR></TABLE>";
	}
}
</SCRIPT>
<body topmargin="4" leftmargin="4">
<form runat="server">
<asp:Literal ID="ReturnLink" runat="server"/>
<BR><BR>
<font class="portalbodytext"><B>RSS News Feed Selection</B></font>
<BR><BR>

<font class="portalbodytext">Enter the URL to any RSS Feed to view<BR>
When you have finished, click <B>Save</B> to save your feed URL.</font>
<BR><BR>

<TABLE cellpadding=3 cellspacing=0 class=portalbodytext>
		<asp:Panel ID="SettingsForm" runat="server">
		<TR><TD colspan=2><B>Sample RSS Resource Sites On The Web</B></TD></TR>
		<TR><TD>New York Times</TD><TD><A HREF="http://www.nytimes.com/services/xml/rss/" Target="_new">http://www.nytimes.com/services/xml/rss/</A></TD></TR>
		<TR><TD>Yahoo!</TD><TD><A HREF="http://news.yahoo.com/rss" Target="_new">http://news.yahoo.com/rss</A></TD></TR>
		<TR><TD>BankRate.com</TD><TD><A HREF="http://www.bankrate.com/brm/rss/entrypage1.asp" Target="_new">http://www.bankrate.com/brm/rss/entrypage1.asp</A></TD></TR>
		<TR><TD colspan=2><BR><BR></TD></TR>
		<TR><TD>RSS Feed Url:</TD><TD><asp:TextBox Width="400px" Text="http://" ID="url" runat="Server"/></TD></TR>
		<TR><TD></TD><TD><asp:Button class="portalbutton" Text="Test Url" OnClick="TestUrl" runat="server"/>&nbsp;&nbsp;<asp:Button class="portalbutton" Text="Save Settings" OnClick="SaveSettings" runat="server"/></TD></TR>
		<TR><TD colspan=2><BR><BR>
		
		<asp:Literal ID="r" runat="Server"/><BR>
		
		<TABLE class=portalbodytext>
		<TR><TD><asp:Literal ID="TitleImage" runat="server"/></TD></TR>
		<TR><TD><B><asp:Literal ID="Title" runat="Server"/></B></TD></TR>
		<TR><TD><asp:Literal ID="Description" runat="Server"/></TD></TR>
		</TABLE>

		<asp:DataGrid AutoGenerateColumns="false" 
		ID="dg" 
		Border="0" 
		runat="server">
		<Columns>
		<asp:TemplateColumn SortExpression="Message" HeaderText="">
		<HeaderStyle>
		</HeaderStyle>
		<ItemStyle VerticalAlign="Top"/>
		<ItemTemplate>
		<IMG SRC="../../news.gif">&nbsp;
		</ItemTemplate>
		</asp:TemplateColumn>
		<asp:TemplateColumn SortExpression="Message" HeaderText="">
		<HeaderStyle>
		</HeaderStyle>
		<ItemTemplate>
		<font style="font-family:Tahoma;font-size:11px;">
		<A style="color:#000080;" href='<%# DataBinder.Eval(Container.DataItem,"link") %>'><%# DataBinder.Eval(Container.DataItem,"title") %></A>
		<BR>
		<%# DataBinder.Eval(Container.DataItem,"description") %><BR><BR>
		</ItemTemplate>
		</asp:TemplateColumn>
		</Columns>
		</asp:DataGrid>
		<%--<asp:xml id="fetchXML" runat="server" TransformSource="../../RSSReader.xsl"></asp:xml>--%>
				
		</TD></TR>
		</asp:Panel>
<TR><TD></TD><TD>
<font Class="portalbodytext"><BR><asp:Literal ID="CreateResult" runat="server"/></font>
</TD></TR></TABLE>

</form></body>
